#include "fullkeyboard.h"
#include <QGridLayout>

FullKeyboard::FullKeyboard(QWidget *parent)
    : TouchKeyboard(parent)
{
    // 构造函数会自动调用父类的初始化
}

void FullKeyboard::createSimpleLayout()
{
    // 简洁模式布局 - 数字计算器样式
    QGridLayout *layout = new QGridLayout(m_keyboardArea);
    layout->setSpacing(8);
    layout->setContentsMargins(5, 5, 5, 5);
    
    // 第一行：清除、退格
    addFunctionKey(layout, 0, 0, "清除", "clear", 2);
    addFunctionKey(layout, 0, 2, "退格", "backspace", 2);
    
    // 第二行：7, 8, 9, ÷
    QStringList row2 = {"7", "8", "9"};
    addKeyRow(layout, 1, row2);
    addFunctionKey(layout, 1, 3, "÷", "/");
    
    // 第三行：4, 5, 6, ×
    QStringList row3 = {"4", "5", "6"};
    addKeyRow(layout, 2, row3);
    addFunctionKey(layout, 2, 3, "×", "*");
    
    // 第四行：1, 2, 3, -
    QStringList row4 = {"1", "2", "3"};
    addKeyRow(layout, 3, row4);
    addFunctionKey(layout, 3, 3, "-", "-");
    
    // 第五行：0, ., +
    addFunctionKey(layout, 4, 0, "0", "0", 2);
    addFunctionKey(layout, 4, 2, ".", ".");
    addFunctionKey(layout, 4, 3, "+", "+");
    
    // 第六行：等号、确认
    addFunctionKey(layout, 5, 0, "=", "=", 2);
    addFunctionKey(layout, 5, 2, "确认", "enter", 2);
}

void FullKeyboard::createFullLayout()
{
    // 全键盘模式布局 - 1:1还原图片布局
    QGridLayout *layout = new QGridLayout(m_keyboardArea);
    layout->setSpacing(4);
    layout->setContentsMargins(6, 6, 6, 6);
    
    // 第一行：q w e r t y u i o p 退格键
    QStringList row1 = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"};
    addKeyRow(layout, 0, row1);
    addFunctionKey(layout, 0, 10, "⌫", "backspace");
    
    // 第二行：a s d f g h j k l 确认键
    QStringList row2 = {"a", "s", "d", "f", "g", "h", "j", "k", "l"};
    addKeyRow(layout, 1, row2);
    addFunctionKey(layout, 1, 10, "✓", "enter");
    
    // 第三行：z x c v b n m , . 隐藏键
    QStringList row3 = {"z", "x", "c", "v", "b", "n", "m", ",", "."};
    addKeyRow(layout, 2, row3);
    addFunctionKey(layout, 2, 10, "↑", "hide");
    
    // 第四行：功能键区域 - 按图片精确布局
    addFunctionKey(layout, 3, 0, "&123", "number_mode", 2);     // 数字模式切换 (占2列)
    addFunctionKey(layout, 3, 2, "🌐", "language");             // 语言切换图标
    addFunctionKey(layout, 3, 3, "简体中文", "language_text", 5); // 语言显示文本 (占5列)
    addFunctionKey(layout, 3, 8, "?", "?");                   // 问号
    addFunctionKey(layout, 3, 9, "⊞", "keyboard_switch", 2);  // 键盘图标 (占2列)
    
    // 设置列的统一宽度 - 按照规范计算
    for (int i = 0; i < 11; ++i) {
        layout->setColumnMinimumWidth(i, 50);
    }
    
    // 设置行的统一高度
    for (int i = 0; i < 4; ++i) {
        layout->setRowMinimumHeight(i, 45);
    }
}

void FullKeyboard::addKeyRow(QGridLayout *layout, int row, const QStringList &keys, int startCol)
{
    for (int i = 0; i < keys.size(); ++i) {
        KeyboardButton *btn = new KeyboardButton(keys[i], keys[i]);
        
        // 根据模式设置按钮大小
        if (m_currentMode == SimpleMode) {
            btn->setMinimumSize(70, 60);
        } else {
            btn->setMinimumSize(50, 45); // 全键盘模式统一按钮大小
        }
        
        connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
        layout->addWidget(btn, row, startCol + i);
    }
}

void FullKeyboard::addFunctionKey(QGridLayout *layout, int row, int col, 
                                 const QString &text, const QString &value, int colSpan)
{
    KeyboardButton *btn = new KeyboardButton(text, value);
    btn->setFunctionKey(true);
    
    // 根据模式和跨列数设置按钮大小
    if (m_currentMode == SimpleMode) {
        if (colSpan == 2) {
            btn->setMinimumSize(148, 60); // 70*2 + 8 (spacing)
        } else {
            btn->setMinimumSize(70, 60);
        }
    } else {
        // 全键盘模式的按钮大小 - 按照规范计算宽度
        if (colSpan == 5) {
            btn->setMinimumSize(254, 45); // 5*50 + 4*4 = 254px (简体中文)
        } else if (colSpan == 2) {
            btn->setMinimumSize(104, 45); // 2*50 + 1*4 = 104px (&123和键盘按钮)
        } else {
            btn->setMinimumSize(50, 45);  // 标准按钮
        }
    }
    
    connect(btn, &KeyboardButton::keyPressed, this, &TouchKeyboard::onKeyPressed);
    layout->addWidget(btn, row, col, 1, colSpan);
}